<?php

class cntrlgrouplist implements viewController {
	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js"), "CSS" => array("facebox.css", "hideSidebar.css") );
	private $pageName = "PicBoard group list";
	private $userObj;
	private $tools = "";

	public function __construct(user &$userObj){
		$this->userObj = $userObj;
		if($this->userObj->getAccessLevel() == -1 || $this->userObj->getAccessLevel() == ""){
			// User is not logged in
			header("Location: home");
		} else {
			$this->area = new template('area.listPage');	
			$this->side = "";
					
			parse_str( $_GET['act'] );
		
			
			$itemTemplate = new template("fragment.listDisplayItem");
			
			// Get the current page
			$page = isset($page) ? (int)$page : 1;
			$start = --$page * SYS_PAGE_MAXITEMS;	

			$charts = group::getGroupList($this->userObj->getuID(), $start, SYS_PAGE_MAXITEMS);	
			
			
			$chartCount = $charts['FOUND_ROWS()'];			
			unset($charts['FOUND_ROWS()']);
	
			$numPages = ceil($chartCount / SYS_PAGE_MAXITEMS);
	
			$pagination = sprintf("<var id=\"lang_222_page\"></var> %d <var id=\"lang_221_of\"></var> %d. %d <var id=\"lang_224_total\" class=\"lower\"></var> <var id=\"lang_122_groups\" class=\"lower\"></var>.", (int)$page+1, $numPages, $chartCount );
						
			$prevLink = (($page+1) == 1) ? "<var id=\"lang_185_previousPage\"></var>" : "<a href='grouplist?page=" . $page . "'><var id=\"lang_185_previousPage\"></var></a>";
			$nextLink = (($page+1) == $numPages) ? "<var id=\"lang_223_nextPage\"></var>" : "<a href='grouplist?page=" . ($page+2) . "'><var id=\"lang_223_nextPage\"></var></a>";
						
			$pageNav = sprintf("$prevLink | <var id=\"lang_222_page\"></var> %d | $nextLink", $page+1);
			
			$chartOutput = $this->formatGroupsAndReturn( $itemTemplate, $charts );	
			
			$this->area->replaceAll(array(
				'list' => $chartOutput,
				'pagination' =>	$pagination,
				'pageNav' => $pageNav
			));		
			
		}
	}


	private function formatGroupsAndReturn(template &$itemTemplate, $groups){
		$groupOutput = "";
		foreach($groups as $group){
			if($group['active'] == 1){
				$out = "<a href='group?id=" . $group['group_gID'] . "'>" . $group['name'] . "</a>";
				$out .= "<p>" . $group['description'] . "</p>";

				$itemTemplate->replace("desc", $out);
				$itemTemplate->replace("image", "");
				$groupOutput .= $itemTemplate->get();
				$itemTemplate->reset();
			}
		}	
		return $groupOutput;
		
	}

	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}

	public function getPageName(){
		return $this->pageName;
	}
	
	public function getTools(){
		return $this->tools;
	}	

}

?>